package com.deyuanyun.pic.settings.controller.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.deyuanyun.pic.common.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *  业务信息阀值设置VO
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-06-27
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class BizMsgThresholdVO implements Serializable {

    private static final long serialVersionUID = -5722104896278776660L;

    private String id;

    @JSONField(serialize = false)
    private String type;//业务阀值类型

    @JSONField(serialize = false)
    private String code;//编号（KEY）

    @JSONField(serialize = false)
    private String name;//名称

    @JSONField(serialize = false)
    private String thresholdContent; //阀值内容：JSON存储

    private String roleId;//角色ID，多个角色ID用逗号分开

    @JSONField(serialize = false)
    private Integer sort;//排序

    @JSONField(serialize = false)
    private String msgContent;//消息内容

    @JSONField(serialize = false)
    private String parentId;//父级id

    private Object thresholdContentJson;//前台需要的json

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getThresholdContent() {
        return thresholdContent;
    }

    public void setThresholdContent(String thresholdContent) {
        this.thresholdContent = thresholdContent;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getMsgContent() {
        return msgContent;
    }

    public void setMsgContent(String msgContent) {
        this.msgContent = msgContent;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public Object getThresholdContentJson() { //因为阀值内容存的是json格式的，所以给转一下，这个json格式为list结构或者map接口
        if (this.thresholdContentJson == null) {
            String thresholdContent = this.thresholdContent;
            if (!StringUtils.isEmpty(thresholdContent)) {
                try {
                    JSONArray jsonArray = JSON.parseArray(thresholdContent);//先尝试转换成list结构
                    return (List) jsonArray;
                } catch (Exception e1) { //在转换失败的时候尝试转换为map结构
                    try {
                        JSONObject jsonObject = JSON.parseObject(thresholdContent);
                        return (Map<String, Object>) jsonObject;
                    } catch (Exception e2) { //再转换失败直接不再进行转换，直接返回空字符串
                        return "";
                    }
                }
            }

            return "";
        } else {
            return this.thresholdContentJson;
        }
    }

    public void setThresholdContentJson(Object thresholdContentJson) {
        this.thresholdContentJson = thresholdContentJson;
    }


    /**
     * 得到角色id集合信息
     * @return
     */
    @JSONField(deserialize = false,serialize = false)
    public List<String> getRoleIdList() {
        if(roleId != null && roleId != "") {
            return Arrays.asList(roleId.split(","));
        }
        return new ArrayList<String>();
    }
}
